<script setup lang="ts">
import { ref } from 'vue'
import { ElInput, ElButton, ElDialog, ElMessage } from 'element-plus'
import DataSourceDialog from '/@/components/DataSourceDialog.vue'
import PushDialog from '/@/components/PushDialog.vue'
import NavItem from '/@/components/NavItem.vue'
import { navItems } from '/@/config/navItems'
import { useNavigation } from '/@/composables/useNavigation'

// 使用导航 composable
const {
  showCreateDialog,
  showDataSourceDialog,
  showPushDialog,
  handleNavItemClick,
  handleDataSourceCreated,
  handlePushCreated
} = useNavigation()

// 其他状态和方法
const searchKeyword = ref('')
const activeTab = ref('recent')
const projectName = ref('')
const selectedColor = ref('#1890ff')

// 项目列表
const projects = ref([
  {
    id: 1,
    name: '测试项目',
    pages: 0,
    views: 1
  }
])

// 颜色选项
const colorOptions = [
  '#1890ff', '#52c41a', '#faad14', '#f5222d', '#722ed1', 
  '#13c2c2', '#eb2f96', '#fadb14', '#a0d911', '#fa541c'
]

// 处理搜索
const handleSearch = () => {
  // TODO: 实现搜索逻辑
  console.log('搜索:', searchKeyword.value)
}

// 处理新建项目
const handleCreateProject = () => {
  showCreateDialog.value = true
  projectName.value = ''
  selectedColor.value = colorOptions[0]
}

// 确认创建项目
const confirmCreate = () => {
  if (!projectName.value.trim()) {
    ElMessage.warning('请输入项目名称')
    return
  }
  
  // TODO: 调用创建项目接口
  const newProject = {
    id: Date.now(),
    name: projectName.value,
    pages: 0,
    views: 0,
    color: selectedColor.value
  }
  
  projects.value.unshift(newProject)
  showCreateDialog.value = false
  ElMessage.success('创建成功')
}

// 处理点击接入数据源
const handleDataSource = () => {
  showDataSourceDialog.value = true
}

// 处理推送配置
const handlePush = () => {
  showPushDialog.value = true
}

// 处理导航项点击
const handleNavigation = (id: number) => {
  switch (id) {
    case 1:
      handleCreateProject()
      break
    case 5:
      handleDataSource()
      break
    case 6:
      handlePush()
      break
    default:
      handleNavItemClick(id)
      break
  }
}
</script>

<template>
  <div class="bi-container">
    <!-- 顶部导航 -->
    <div class="nav-container">
      <div class="nav-items">
        <NavItem
          v-for="item in navItems"
          :key="item.id"
          :icon="item.icon"
          :text="item.text"
          :color="item.color"
          @click="() => handleNavigation(item.id)"
        />
      </div>
    </div>

    <!-- 主要内容区域 -->
    <div class="main-content">
      <!-- 标签和搜索区 -->
      <div class="header-section">
        <div class="tabs">
          <div 
            class="tab" 
            :class="{ active: activeTab === 'recent' }"
            @click="activeTab = 'recent'"
          >
            最近访问
          </div>
          <div 
            class="tab" 
            :class="{ active: activeTab === 'my' }"
            @click="activeTab = 'my'"
          >
            我创建的
          </div>
        </div>
        <div class="search-box">
          <el-input
            v-model="searchKeyword"
            placeholder="输入关键词搜索"
            @keyup.enter="handleSearch"
          >
            <template #suffix>
              <i class="fa fa-search search-icon"></i>
            </template>
          </el-input>
        </div>
      </div>

      <!-- 项目列表区域 -->
      <div class="projects-section">
        <div class="projects-header">
          <h2>我的项目</h2>
          <el-button type="primary" @click="handleCreateProject">
            <i class="fa fa-plus"></i>
            新建项目
          </el-button>
        </div>

        <div class="projects-list">
          <div v-for="project in projects" :key="project.id" class="project-card">
            <div class="project-icon">
              <i class="fa fa-folder"></i>
            </div>
            <div class="project-info">
              <h3>{{ project.name }}</h3>
              <div class="project-stats">
                <span>页面: {{ project.pages }}</span>
                <span>成员: {{ project.views }}</span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 添加创建项目对话框 -->
    <el-dialog
      v-model="showCreateDialog"
      title="创建项目"
      width="500px"
      :close-on-click-modal="false"
    >
      <div class="create-project-form">
        <div class="form-item">
          <div class="label">项目名称 <span class="required">*</span></div>
          <el-input
            v-model="projectName"
            placeholder="请输入2~30个字（英文或汉字）"
            maxlength="30"
            show-word-limit
          />
        </div>
        
        <div class="form-item">
          <div class="label">项目图标</div>
          <div class="project-icon-preview" :style="{ background: selectedColor }">
            项
          </div>
          <div class="color-options">
            <div
              v-for="color in colorOptions"
              :key="color"
              class="color-item"
              :class="{ active: selectedColor === color }"
              :style="{ background: color }"
              @click="selectedColor = color"
            />
          </div>
        </div>
        
        <div class="form-item">
          <div class="label">创建方式</div>
          <div class="create-type">
            <div class="type-item active">
              <i class="fa fa-plus-circle"></i>
              <span>创建空项目</span>
            </div>
            <div class="type-item disabled">
              <i class="fa fa-database"></i>
              <span>导入资源包</span>
            </div>
          </div>
        </div>
      </div>
      
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showCreateDialog = false">取消</el-button>
          <el-button type="primary" @click="confirmCreate">
            创建并进入管理
          </el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 数据源配置对话框 -->
    <DataSourceDialog
      v-model:visible="showDataSourceDialog"
      @confirm="handleDataSourceCreated"
    />

    <!-- 添加推送配置对话框 -->
    <PushDialog
      v-model:visible="showPushDialog"
      @confirm="handlePushCreated"
    />
  </div>
</template>

<style scoped>
.bi-container {
  padding: 20px;
  background-color: #f5f7fa;
  min-height: 100vh;
}

.nav-container {
  background: white;
  padding: 24px;
  border-radius: 12px;
  margin-bottom: 24px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.nav-items {
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
}

.main-content {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.header-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #e4e7ed;
}

.tabs {
  display: flex;
  gap: 24px;
}

.tab {
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 4px;
  color: #606266;
  transition: all 0.3s;
}

.tab:hover {
  color: #409eff;
}

.tab.active {
  color: #409eff;
  background: #ecf5ff;
}

.search-box {
  width: 300px;
}

.search-icon {
  color: #909399;
  cursor: pointer;
}

.projects-section {
  padding: 20px 0;
}

.projects-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.projects-header h2 {
  margin: 0;
  font-size: 18px;
  color: #303133;
}

.projects-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.project-card {
  display: flex;
  align-items: center;
  padding: 16px;
  background: #f8f9fa;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.project-card:hover {
  background: #f0f2f5;
  transform: translateY(-2px);
}

.project-icon {
  width: 48px;
  height: 48px;
  background: #e6f0ff;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 16px;
}

.project-icon i {
  font-size: 24px;
  color: #409eff;
}

.project-info {
  flex: 1;
}

.project-info h3 {
  margin: 0 0 8px 0;
  font-size: 16px;
  color: #303133;
}

.project-stats {
  display: flex;
  gap: 16px;
  color: #909399;
  font-size: 14px;
}

.create-project-form {
  padding: 20px 0;
}

.form-item {
  margin-bottom: 24px;
}

.form-item .label {
  margin-bottom: 8px;
  color: #606266;
  font-size: 14px;
}

.required {
  color: #f56c6c;
  margin-left: 4px;
}

.project-icon-preview {
  width: 60px;
  height: 60px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  margin-bottom: 16px;
}

.color-options {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.color-item {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
}

.color-item:hover {
  transform: scale(1.1);
}

.color-item.active::after {
  content: '';
  position: absolute;
  top: -2px;
  left: -2px;
  right: -2px;
  bottom: -2px;
  border: 2px solid currentColor;
  border-radius: 6px;
  color: inherit;
}

.create-type {
  display: flex;
  gap: 16px;
}

.type-item {
  flex: 1;
  padding: 16px;
  border: 1px solid #e4e7ed;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  transition: all 0.3s;
}

.type-item.active {
  border-color: #409eff;
  background: #ecf5ff;
  color: #409eff;
}

.type-item.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.type-item i {
  font-size: 24px;
}

.type-item span {
  font-size: 14px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  padding-top: 20px;
}
</style>
